package HP.com.bank_1.wangyu;

import java.util.Calendar;
import java.util.Scanner;

/**
 * @author 王羽
 *
 */
public class Tools {
    //加载测试数据
    public static void init() {
        //初始化管理员账号
        BankMain.no[0] = "admin";
        BankMain.pwd[0] = "admin";
        BankMain.mon[0] = 0;
        BankMain.log[0] ="----------------日志----------------\n"+"账 户 名: "+BankMain.no[0]+"\n开户时间: "+getDate()+"\n开户金额: "+0+"元"+"\n------------------------------------";//"----------日志----------\n"+"账 户 名: "+BankMain.no[0];

        BankMain.no[1] = "王羽";
        BankMain.pwd[1] = "wangyu";
        BankMain.mon[1] = 1005.0;
        BankMain.log[1] = "----------------日志----------------\n"+"账 户 名: "+BankMain.no[1]+"\n开户时间: "+getDate()+"\n开户金额: "+1005.0+"元"+"\n------------------------------------";

        BankMain.no[2] = "hp";
        BankMain.pwd[2] = "hp";
        BankMain.mon[2] = 1003.0;
        BankMain.log[2] = "----------------日志----------------\n"+"账 户 名: "+BankMain.no[2]+"\n开户时间: "+getDate()+"\n开户金额: "+1003.0+"元"+"\n------------------------------------";

        BankMain.no[3] = "wangyu";
        BankMain.pwd[3] = "wangyu";
        BankMain.mon[3] = 99999.0;
        BankMain.log[3] = "----------------日志----------------\n"+"账 户 名: "+BankMain.no[2]+"\n开户时间: "+getDate()+"\n开户金额: "+99999.0+"元"+"\n------------------------------------";
    }

    //欢迎页
    public static void welcome() {
        printUI(0);
        Scanner scanner = new Scanner(System.in);
        printUI(3);
        printUI(1);
        System.out.println("请输入1--3进行选择:");
        try {
            int welcome = scanner.nextInt();
            switch (welcome) {
                case 1://登陆
                    //printUI(0);
                    login();
                    break;
                case 2://注册
                    reg();
                    break;
                case 3://更新日志
                    printUI(6);
                    printUI(1);
                    break;
                case 4:
                    printUI(2);
                    break;
            }
        } catch (Exception e) {
            System.out.println("输入有误!");
        }

    }

    //业务菜单
    private static void mainMenu() {
        printUI(0);
        if (BankMain.userlogin == 0) {
           adminMenu();
    } else {
            showUser(BankMain.userlogin);
            printUI(4);
            Scanner sca_menu = new Scanner(System.in);
            printUI(1);
            System.out.println("输入1--5进行操作:");
            try {
                int input_menu = sca_menu.nextInt();
                switch (input_menu) {
                    case 1://存钱
                        double money = inputMoney(BankMain.userlogin);
                        System.out.println(money + "元以存入您的账户,当前余额:" + chaXun(BankMain.userlogin) + "元");
                        mainMenu();
                        break;
                    case 2://取钱
                        outMoney(BankMain.userlogin);
                        System.out.println("当前账户余额:" + chaXun(BankMain.userlogin) + "元");
                        mainMenu();
                        break;
                    case 3://查询
                        System.out.println("您的余额为" + chaXun(BankMain.userlogin) + "元");
                        userLog(BankMain.userlogin,4,-1,-1);
                        printUI(1);
                        mainMenu();
                        break;
                    case 4://转账
                        moveMoney(BankMain.userlogin);
                        mainMenu();
                        break;
                    case 5://修改密码
                        changePassword(BankMain.userlogin);
                        mainMenu();
                        break;
                    case 6://查看日志
                        showLog(BankMain.userlogin);
                        userLog(BankMain.userlogin,7,-1,-1);
                        mainMenu();
                        break;
                    case 7://退出
                        userLog(BankMain.userlogin,8,-1,-1);
                        BankMain.userlogin = -1;
                        System.out.println("感谢使用~~");
                        welcome();
                        break;
                }
            } catch (Exception e) {
                System.out.println("输入有误!");
            }
        }
    }

    //登录
    private static void login() {
        printUI(0);
        Scanner scanner_login = new Scanner(System.in);
        boolean flag = false;
        System.out.println("当前位置-->登录\n请输入用户名和密码进行登录.\n提示:管理员账号\\密码:admin");
        System.out.println("用户名:");
        String user_no = scanner_login.nextLine();
        System.out.println("密码:");
        String user_pwd = scanner_login.nextLine();

        for (int i = 0; i < BankMain.no.length; i++) {
            // System.out.println("验证密码:");
            int isadmin = -1;
            if (user_no.equals(BankMain.no[i]) && user_pwd.equals(BankMain.pwd[i])) {
                if (user_no.equals("admin")) {
                    flag = true;
                    BankMain.userlogin = 0;
                    break;
                } else {
                    flag = true;
                    //System.out.println(BankMain.no[i]);
                    //System.out.println(BankMain.pwd[i]);
                    //loginuser = i;
                    BankMain.userlogin = i;
                    // System.out.println("i = " + i);
                    userLog(i,0,-1,-1);
                    break;

                }
            }
        }
        if (flag) {
            System.out.println("登录成功!");
            printUI(1);
            mainMenu();
        } else {
            System.out.println("登录失败!");
            printUI(1);
        }
    }

    //注册
    private static void reg() {
        boolean flag = false;
        printUI(0);
        System.out.println("当前位置-->注册");
        for (int i = 0; i < BankMain.no.length; i++) {
            if (BankMain.no[i] == null) {
                Scanner sca_reg = new Scanner(System.in);
                System.out.println("请根据提示完成注册.");
                System.out.println("请输入账户名:");
                String input_no = sca_reg.nextLine();
                if (chaXunXiaBiao(input_no) != -1) {
                    System.out.println("该账户名已被使用,请重新注册!");
                    welcome();
                }
                System.out.println("请输入密码:");
                String input_pwd = sca_reg.nextLine();
                System.out.println("请再次输入密码:");
                String input_pwd1 = sca_reg.nextLine();
                if (!(input_pwd.equals(input_pwd1))) {
                    System.out.println("两次输入的密码不一致,请重新注册!");
                    welcome();
                }
                System.out.println("输入预存金额:");
                double intput_mon = sca_reg.nextDouble();
                //写入数组
                BankMain.no[i] = input_no;
                BankMain.pwd[i] = input_pwd;
                BankMain.mon[i] = intput_mon;
                BankMain.log[i] = "";

                //chackArrey();
                if ((input_no.equals(BankMain.no[i])) && (input_pwd.equals(BankMain.pwd[i])) && (intput_mon == BankMain.mon[i])) {
                    flag = true;
                    userLog(i,1,-1,intput_mon);
                    break;
                }
            } else {
                // System.out.println("越界!");
            }
        }
        if (flag) {
            System.out.println("注册成功,请登录激活账户!");
            welcome();
        } else {
            System.out.println("注册失败!");
            welcome();
        }
    }

    //遍历数组
    private static void chackArrey() {
        /**
         * ---------日志---------
         * 账 户 名: wangyu
         * 开户时间: 2017年8月15日
         * ---------------------
         * 业务:登录
         * 时间:2017年8月15日
         * ---------------------
         * 业务:修改密码
         * 时间:2017年8月15日
         * ---------------------
         * 业务:向 wangyu 转账100元
         * 时间:2017年8月15日
         * ---------------------
         */
        System.out.println("----------------日志----------------");
        for (int i = 0; i < BankMain.no.length; i++) {
            if (BankMain.no[i] != null) {
                System.out.println("账户名:"+BankMain.no[i]);
                System.out.println("密  码:"+BankMain.pwd[i]);
                System.out.println("金  额:"+BankMain.mon[i]);
                System.out.println("----------------------------------------------");
            }
        }
    }

    //存款
    private static double inputMoney(int userlogin) {
        printUI(0);
        System.out.println("当前位置-->业务菜单");
        Scanner sca_inmoney = new Scanner(System.in);
        System.out.println("输入存款金额:");
        double intput_mon = sca_inmoney.nextDouble();
        if (intput_mon > 0) {
            BankMain.mon[userlogin] += intput_mon;
            userLog(userlogin,2,-1,intput_mon);//生成日志
            return intput_mon;
        } else {
            System.out.println("金额必须大于0");
            return 0;
        }
    }

    //取款
    private static void outMoney(int userlogin) {
        printUI(0);
        System.out.println("当前位置-->取款");
        Scanner sca_outmoney = new Scanner(System.in);
        System.out.println("当前账户余额:" + chaXun(userlogin) + "元");
        System.out.println("输入取款金额:");
        double output_mon = sca_outmoney.nextDouble();

        if (userlogin == -1) {
            System.out.println("取款userlogin = " + userlogin);
        } else if (output_mon > chaXun(userlogin)) {
            System.out.println("账户余额不足!");
        } else if (output_mon == 0) {
            System.out.println("取款金额需要大于0");
        } else {
            BankMain.mon[userlogin] = BankMain.mon[userlogin] - output_mon;
            userLog(userlogin,3,-1,output_mon);//生成日志
            System.out.println("取款成功!");
        }
    }

    //查询余额
    private static double chaXun(int userlogin) {
        if (userlogin == -1) {
            System.out.println("查询不到余额");
            return 0.00;
        } else
            return BankMain.mon[userlogin];
    }

    //转账
    private static void moveMoney(int userlogin) {
        try {
            printUI(0);
            System.out.println("当前位置-->转账");
            Scanner sca_movemoney = new Scanner(System.in);
            System.out.println("当前账户余额:" + chaXun(userlogin) + "元");
            System.out.println("输入转账金额:");
            double move_mon = sca_movemoney.nextDouble();

            if (userlogin == -1) {
            } else if (move_mon > chaXun(userlogin)) {
                System.out.println("账户余额不足!");
                mainMenu();
            } else if (move_mon == 0) {
                System.out.println("转账金额需要大于0");
                mainMenu();
            } else {
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入对方账户:");
                String move_no = scanner.nextLine();

                int move_xiabiao = chaXunXiaBiao(move_no);
                if (move_xiabiao != -1) {
                    BankMain.mon[userlogin] = BankMain.mon[userlogin] - move_mon;
                    BankMain.mon[move_xiabiao] = BankMain.mon[move_xiabiao] + move_mon;
                    System.out.println("转账成功!");
                    System.out.println("您的当前账户余额:" + chaXun(userlogin) + "元");
                    //记录日志
                    userLog(userlogin,5,move_xiabiao,move_mon);
                }else {
                    System.out.println("此账户不存在,转账失败!");
                    mainMenu();
                }
            }
        }catch (Exception e){
            System.out.println("输入有误!");
            mainMenu();
        }

    }

    //通过账户名查找下表
    private static int chaXunXiaBiao(String no) {
        int xiabiao = -1;
        for (int i = 0; i < BankMain.no.length; i++) {
            if (no.equals(BankMain.no[i])) {
                xiabiao = i;
                break;
            }
        }
        return xiabiao;
    }

    //通过下表查找用户名
    private static String serchUser(int userlogin) {
        return BankMain.no[userlogin];
    }

    //显示当前用户
    private static void showUser(int userlogin) {
        System.out.println("当前用户-->" + BankMain.no[userlogin]);
    }

    //生成日志
    private static void userLog(int userlogin, int yewu,int other,double money) {
        /**
         * userlogin :下表; yewu :操作类型 ;other = -1 表示没有备注 ;money = -1:表示没有此项;
         *   String log = "时间:" + getDate() + "\n" + "操作:登录\n";
         userLog(BankMain.userlogin, log);
         */
        String data_str = "\n操作时间: ";
        String date = getDate();
        String lin = "\n------------------------------------";

        String yewu_str = "\n业务类型: ";

        switch (yewu){
            case 0://登录
                String login_log = yewu_str +"登录"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+login_log;

                //显示登录日志
                //System.err.println(BankMain.log[userlogin]);
                break;
            case 1://注册
//                String reg_log ="----------------日志----------------\n"+"账 户 名: "+BankMain.no[userlogin]+"\n开户时间: "+date+lin;
                String reg_log ="----------------日志----------------\n"+"账 户 名: "+BankMain.no[userlogin]+"\n开户时间: "+date+"\n开户金额: "+money+"元"+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+reg_log;

                //显示注册日志
                //System.err.println(BankMain.log[userlogin]);
                break;
            case 2://存款
                String inmoney_log  = yewu_str +"存款"+money+"元"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+inmoney_log;
                //显示存款日志
               // System.err.println(BankMain.log[userlogin]);
                break;
            case 3://取款
                String outmoney_log  = yewu_str +"取款"+money+"元"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+outmoney_log;
                //显示取款日志
               // System.err.println(BankMain.log[userlogin]);
                break;
            case 4://查询余额
                String showmoney_log  = yewu_str +"查询余额"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+showmoney_log;
                //显示查询余额日志
               // System.err.println(BankMain.log[userlogin]);
                break;
            case 5://转账
                //向某人转账
                String movemoney_log  = yewu_str +"向"+BankMain.no[other]+"转账"+money+"元"+data_str+date+lin;
                BankMain.log[userlogin] = BankMain.log[userlogin]+movemoney_log;
                //收到某人转账
                String receivemoney_log  = yewu_str +BankMain.no[userlogin]+"向您转账"+money+"元"+data_str+date+lin;
                BankMain.log[other] = BankMain.log[other]+receivemoney_log;
                //yewu_str +"收到"+BankMain.no[other]+"转账"+money+"元"+data_str+date+lin;
                //显示转账日志
               // System.out.println("我的日志:");
               // System.err.println(BankMain.log[userlogin]);
               // System.out.println("对方日志:");
               // System.err.println(BankMain.log[other]);
                break;
            case 6://修改密码
                String pwd_log = yewu_str +"修改密码"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+pwd_log;

                //显示密码修改日志
                //System.err.println(BankMain.log[userlogin]);
                break;
            case 7://查看日志
                String show_log = yewu_str +"查看日志"+data_str+date+lin;
                BankMain.log[userlogin] =  BankMain.log[userlogin]+show_log;

                //显示密码修改日志
                //System.err.println(BankMain.log[userlogin]);
                break;
            case 8://退出系统
                String offline_log = yewu_str +"退出系统"+data_str+date+lin;
                BankMain.log[userlogin] = BankMain.log[userlogin]+offline_log;

                //显示退出系统日志
                //System.err.println(BankMain.log[userlogin]);
                break;
        }
        /**
         * ---------日志---------
         * 账 户 名: wangyu
         * 开户时间: 2017年8月15日
         * ---------------------
         * 业务:登录
         * 时间:2017年8月15日
         * ---------------------
         * 业务:修改密码
         * 时间:2017年8月15日
         * ---------------------
         * 业务:向 wangyu 转账100元
         * 时间:2017年8月15日
         * ---------------------
         *
         *
         String no = "wangyu";
         String date = "2017年8月15日";
         String lin = "\n------------------------";
         String yewu1  = "登录";
         String yewu2  = "修改密码";
         String yewu3  = "";
         System.out.println("----------日志----------\n"+"账 户 名: "+no+"\n开户时间: "+date+lin+"\n业 务:"+yewu1+"\n时 间:"+date+lin);
         }
         */
  /*     System.out.println();
        BankMain.log[userlogin] += log;*/
    }

    //查看日志
    private static void showLog(int userlogin) {
        System.out.println(BankMain.log[userlogin]);
    }

    //获取当前时间
    private static String getDate() {
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);//获取年份
        int month = ca.get(Calendar.MONTH);//获取月份
        int day = ca.get(Calendar.DATE);//获取日
        int minute = ca.get(Calendar.MINUTE);//分
        int hour = ca.get(Calendar.HOUR);//小时
        int second = ca.get(Calendar.SECOND);//秒

        String date = year + "年" + (month + 1) + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒";
        return date;
    }

    //绘制UI
    private static void printUI(int ui) {
        if (ui == 0) {
            System.out.println("---------欢迎使用Dos Mini Bank系统1.0----------");
        } else if (ui == 1) {
            System.out.println("----------------------------------------------");
        } else if (ui == 2) {
            System.out.println("制 作 人: 王羽");
            System.out.println("制作公司: 河南洛阳惠普人才有限公司");
            System.out.println("公司网址: www.hp.bank.com");
            System.out.println("公司邮箱: admin@hp.bank.com");
            printUI(1);
        } else if (ui == 3) {
            System.out.println("\t\t\t\t1.用户登录");
            System.out.println("\t\t\t\t2.用户注册");
            System.out.println("\t\t\t\t3.更新日志");
            System.out.println("\t\t\t\t4.关于");
        } else if (ui == 4) {
            System.out.println("当前位置-->业务菜单");
            System.out.println("\t\t\t\t1.存款");
            System.out.println("\t\t\t\t2.取款");
            System.out.println("\t\t\t\t3.查询余额");
            System.out.println("\t\t\t\t4.转账");
            System.out.println("\t\t\t\t5.修改密码");
            System.out.println("\t\t\t\t6.查看日志");
            System.out.println("\t\t\t\t7.退出");
        } else if (ui == 5) {
            System.out.println("欢迎您,管理员!");
            System.out.println("当前位置-->管理员菜单");
            System.out.println("\t\t\t\t1.查看数据库");
            System.out.println("\t\t\t\t2.销户");
            System.out.println("\t\t\t\t3.退出");
            //System.out.println("\t\t\t\t?.查看用户日志");
            //System.out.println("\t\t\t\t?.查看管理员日志");
            System.out.println("\t\t\t\t4.关机");
        }else if (ui == 6) {
            System.out.println("请反馈Bug至邮箱:w950888@139.com");
            System.out.println("版本:1.6");
            System.out.println("---------------更新日志--------------");
            System.out.println("****************8.16****************");
            System.out.println("1.完善记录用户日志功能");
            System.out.println("2.修复一个登陆Bug");
            System.out.println("3.修复转账输入错误的Bug");
            System.out.println("4.添加管理员关机功能");
            System.out.println("5.添加管理员删除用户的功能");
            System.out.println("****************8.17****************");
        }
    }

    //修改密码
    private static void changePassword(int userLogin) {
        System.out.println("请输入旧密码:");
        Scanner scanner = new Scanner(System.in);
        String old_pwd = scanner.nextLine();
        if (old_pwd.equals(BankMain.pwd[userLogin])) {
            //System.out.println("请输入");
            System.out.println("请输入新密码:");
            String input_pwd = scanner.nextLine();
            System.out.println("请再次输入新密码:");
            String input_pwd1 = scanner.nextLine();
            if (!(input_pwd.equals(input_pwd1))) {
                System.out.println("两次输入的密码不一致,请重新修改密码!");
                mainMenu();
            }
            BankMain.pwd[userLogin] = input_pwd;
            //System.out.println("BankMain.pwd[userLogin] = " + BankMain.pwd[userLogin]);
            System.out.println("密码修改成功!");
            //记录日志
            userLog(userLogin,6,-1,-1);
        } else {
            System.out.println("密码错误!");
        }
    }

    //管理员菜单
    private static void adminMenu(){
        //printUI(0);
        printUI(5);
        Scanner sca_menu = new Scanner(System.in);
        printUI(1);
        System.out.println("输入1--3进行操作:");
        try {
            int input_menu = sca_menu.nextInt();
            switch (input_menu) {
                case 1://查看数据库
                    chackArrey();
                    mainMenu();
                    break;
                case 2://注销账户
                    delUser();
                    break;
                case 3://退出
                    BankMain.userlogin = -1;
                    System.out.println("感谢使用~~");
                    welcome();
                    mainMenu();
                    break;
                case 4://关机
                    shtudown();
                    System.exit(0);
                    break;
                case 5://退出

                    break;
                case 6://退出

                    break;
            }
        } catch (Exception e) {
            System.out.println("输入有误!");
        }

    }

    //注销账户
    private static void delUser(){
        try {
            System.out.println("请输入需要删除的账户名:");
            Scanner scanner = new Scanner(System.in);
            String in_no = scanner.nextLine();
            int xiabiao = chaXunXiaBiao(in_no);
            System.out.println("xiabiao = " + xiabiao);
            if (xiabiao==-1){
                System.out.println("此账户不存在!");
                mainMenu();
            }else {
                System.out.println("xiabiao = " + xiabiao);
                System.out.println("确认删除?(y/n)");
                String y_n = scanner.nextLine();
                if (y_n.equals("y")){
                    BankMain.no[xiabiao]= null;
                    BankMain.pwd[xiabiao]= null;
                    BankMain.mon[xiabiao] = 0;
                    BankMain.log[xiabiao]= null;
                    System.out.println("删除成功!");
                    mainMenu();
                }else if (y_n.equals("n")){
                    mainMenu();
                }else {
                    System.out.println("输入错误!");
                    mainMenu();
                }
            }
        }catch (Exception e){
            System.out.println("输入有误!");
            mainMenu();
        }
    }

    //关机
    private static void shtudown(){
            try{
        Runtime.getRuntime().exec("cmd /c start call shutdown -s -f -t 1");
    }catch(Exception e){
        e.printStackTrace();
    }
    }
}
